package org.examples;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.locks.ReadWriteLock;

@SpringBootApplication
@RestController
public class RedisApplication {
    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class,args);
    }

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/redisTest")
    public void testRedissonLock(){
        List<RedissonClient> list = (List<RedissonClient>) applicationContext.getBean(RedissonClient.class);
//        RLock lock = redissonClient.getLock("test_lock");
        ReadWriteLock lock = redissonClient.getReadWriteLock("test_lock");//读写锁
//        lock.lock();
        lock.writeLock().lock();
        try {
            System.out.println("加锁成功，执行业务..." + Thread.currentThread().getId());
        }finally {
//            lock.unlock();
            lock.writeLock().unlock();
        }
    }
}
